package cn.tedu.travelsystem.scenicSpot.controller;

import cn.tedu.travelsystem.base.response.JsonResult;
import cn.tedu.travelsystem.base.response.StatusCode;
import cn.tedu.travelsystem.base.utils.AccessLimitUtil;
import cn.tedu.travelsystem.scenicSpot.mapper.ScenicSpotMapper;
import cn.tedu.travelsystem.scenicSpot.pojo.dto.ScenicSpotDTO;
import cn.tedu.travelsystem.scenicSpot.pojo.entity.ScenicSpot;
import cn.tedu.travelsystem.scenicSpot.pojo.vo.ScenicSpotVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/scenic-spots")
@Api(tags = "04.景区模块")
public class ScenicSpotController {

    @Autowired
    private ScenicSpotMapper scenicSpotMapper;

    @Autowired
    private AccessLimitUtil accessLimitUtil; // 引入访问限制工具类

    // 添加景区信息
    @PostMapping
    @ApiOperation("添加景区信息")
    @ApiOperationSupport(order = 10)
    public JsonResult addScenicSpot(@Validated @RequestBody ScenicSpotDTO dto, HttpServletRequest request) {
        // 异常访问检测
        String ip = request.getRemoteAddr();
        String key = "limit:ip:" + ip;
        if (accessLimitUtil.isFrequent(key, 20, 60)) {
            // 发送警告信息
            log.warn("IP [{}] 频繁访问，60秒内请求超过限制", ip);
            return new JsonResult(StatusCode.REQUEST_TOO_FREQUENT);
        }

        log.debug("ScenicSpotDTO = " + dto);
        ScenicSpot scenicSpot = new ScenicSpot();
        BeanUtils.copyProperties(dto, scenicSpot);
        int rows = scenicSpotMapper.insert(scenicSpot);
        if (rows > 0) {
            return JsonResult.ok();
        } else {
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
    }

    // 删除景区信息
    @DeleteMapping("/{spotId}")
    @ApiOperation("删除景区信息")
    @ApiOperationSupport(order = 20)
    public JsonResult deleteScenicSpot(@PathVariable Integer spotId, HttpServletRequest request) {
        // 异常访问检测
        String ip = request.getRemoteAddr();
        String key = "limit:ip:" + ip;
        if (accessLimitUtil.isFrequent(key, 20, 60)) {
            // 发送警告信息
            log.warn("IP [{}] 频繁访问，60秒内请求超过限制", ip);
            return new JsonResult(StatusCode.REQUEST_TOO_FREQUENT);
        }

        log.debug("spotId = " + spotId);
        int rows = scenicSpotMapper.deleteById(spotId);
        if (rows > 0) {
            return JsonResult.ok();
        } else {
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
    }

    // 更新景区信息
    @PutMapping("/{spotId}")
    @ApiOperation("更新景区信息")
    @ApiOperationSupport(order = 30)
    public JsonResult updateScenicSpot(@PathVariable Integer spotId, @Validated @RequestBody ScenicSpotDTO dto, HttpServletRequest request) {
        // 异常访问检测
        String ip = request.getRemoteAddr();
        String key = "limit:ip:" + ip;
        if (accessLimitUtil.isFrequent(key, 20, 60)) {
            // 发送警告信息
            log.warn("IP [{}] 频繁访问，60秒内请求超过限制", ip);
            return new JsonResult(StatusCode.REQUEST_TOO_FREQUENT);
        }

        log.debug("spotId = " + spotId + ", ScenicSpotDTO = " + dto);
        ScenicSpot scenicSpot = new ScenicSpot();
        BeanUtils.copyProperties(dto, scenicSpot);
        scenicSpot.setSpot_id(spotId);
        int rows = scenicSpotMapper.update(scenicSpot);
        if (rows > 0) {
            return JsonResult.ok();
        } else {
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
    }

    // 查询单个景区信息
    @GetMapping("/{spotId}")
    @ApiOperation("查询单个景区信息")
    @ApiOperationSupport(order = 40)
    public JsonResult getScenicSpot(@PathVariable Integer spotId, HttpServletRequest request) {
        // 异常访问检测
        String ip = request.getRemoteAddr();
        String key = "limit:ip:" + ip;
        if (accessLimitUtil.isFrequent(key, 20, 60)) {
            // 发送警告信息
            log.warn("IP [{}] 频繁访问，60秒内请求超过限制", ip);
            return new JsonResult(StatusCode.REQUEST_TOO_FREQUENT);
        }

        log.debug("spotId = " + spotId);
        ScenicSpot scenicSpot = scenicSpotMapper.selectById(spotId);
        if (scenicSpot != null) {
            ScenicSpotVO vo = new ScenicSpotVO();
            BeanUtils.copyProperties(scenicSpot, vo);
            return JsonResult.ok(vo);
        } else {
            return new JsonResult(StatusCode.DATA_NOT_FOUND);
        }
    }

    // 查询所有景区信息
    @GetMapping
    @ApiOperation("查询所有景区信息")
    @ApiOperationSupport(order = 50)
    public JsonResult getAllScenicSpots(HttpServletRequest request) {
        // 异常访问检测
        String ip = request.getRemoteAddr();
        String key = "limit:ip:" + ip;
        if (accessLimitUtil.isFrequent(key, 20, 60)) {
            // 发送警告信息
            log.warn("IP [{}] 频繁访问，60秒内请求超过限制", ip);
            return new JsonResult(StatusCode.REQUEST_TOO_FREQUENT);
        }

        log.debug("查询所有景区信息");
        List<ScenicSpot> scenicSpots = scenicSpotMapper.selectAll();
        List<ScenicSpotVO> vos = new ArrayList<>();
        for (ScenicSpot scenicSpot : scenicSpots) {
            ScenicSpotVO vo = new ScenicSpotVO();
            BeanUtils.copyProperties(scenicSpot, vo);
            vos.add(vo);
        }
        return JsonResult.ok(vos);
    }
}
